﻿Imports System.Threading.Tasks
Imports Windows.System
Imports Microsoft.Phone.Tasks
Imports Windows.Phone.Devices.Power
Imports Microsoft.Phone.Net.NetworkInformation
Imports Microsoft.Phone.Shell
Imports Microsoft.Phone.Info

Namespace Utility
    Public Module SysHelper
        Public Async Function CallTo(phone As String) As Task
            Await Launcher.LaunchUriAsync(New Uri("tel:" & phone))
        End Function

        Public Async Function MailTo(email As String) As Task
            Await Launcher.LaunchUriAsync(New Uri("mailto:" & email))
        End Function

        Public Async Function OpenWifi() As Task
            Await Launcher.LaunchUriAsync(New Uri("ms-settings-wifi:"))
        End Function

        Public Async Function OpenBluetooth() As Task
            Await Launcher.LaunchUriAsync(New Uri("ms-settings-bluetooth:"))
        End Function

        ''' <summary>
        ''' 启动手机网络设置
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Async Function OpenCellular() As Task
            Await Launcher.LaunchUriAsync(New Uri("ms-settings-cellular:"))
        End Function

        Public Async Function OpenLocation() As Task
            Await Launcher.LaunchUriAsync(New Uri("ms-settings-location:"))
        End Function

        Public Sub SendSms(content As String, Optional phone As String = "")
            Dim sms As New SmsComposeTask With {.Body = content, .To = phone}
            sms.Show()
        End Sub

        Public Function ConvertToMB(x As Long) As String
            Return Math.Round(x / (1024 * 1024), 2) & "M"
        End Function

        ''' <summary>
        ''' 获取电池相关信息
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetBatteryInfo() As Battery
            Return Battery.GetDefault
        End Function

        ''' <summary>
        ''' 判断是否联网
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function IsNetwork() As Boolean
            Return DeviceNetworkInformation.IsNetworkAvailable
        End Function

        Public Function IsWifi() As Boolean
            Return DeviceNetworkInformation.IsWiFiEnabled
        End Function

        ''' <summary>
        ''' 网络类型
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetNetworkType() As NetworkInterfaceType
            Return NetworkInterface.NetworkInterfaceType
        End Function

        Public Sub OpenUrlWithIE(url As String)
            Dim webTask As New WebBrowserTask
            webTask.Uri = New Uri(url, UriKind.Absolute)
            webTask.Show()
        End Sub

        Public Sub PinToStart(title As String, image As String, backTitle As String, backContent As String, backImage As String, url As String, Optional count As Integer = 0)
            Dim old = ShellTile.ActiveTiles.FirstOrDefault(Function(e) e.NavigationUri.ToString.Contains(url))
            If old IsNot Nothing Then
                old.Delete()
            End If

            Dim nt As New StandardTileData With {
                .BackgroundImage = New Uri(image, UriKind.Relative),
                .Title = title,
                .Count = count,
                .BackTitle = backTitle,
                .BackContent = backContent,
                .BackBackgroundImage = New Uri(backImage, UriKind.Relative)
            }

            ShellTile.Create(New Uri(url, UriKind.Relative), nt)
        End Sub

        Public Function GetDeviceUniqueId() As String
            Dim result = CType(DeviceExtendedProperties.GetValue("DeviceUniqueId"), Byte())
            If result IsNot Nothing Then
                Return Convert.ToBase64String(result)
            Else
                Return String.Empty
            End If
        End Function
    End Module
End Namespace